//ScreenPrint.c
#include "ScreenPrint.h"
#include "ascii.h"
#include "USER_CFG.h"
#include "v4a_hal.h"
#include <string.h>
extern unsigned short GUI_LCM_XMAX,GUI_LCM_YMAX;
extern _enum_DMODE G_DispMode;
extern unsigned char userbkgnd_bmp[1366*768*2];

unsigned int Cursor_x,Cursor_y;
extern IMGNODE bkgnd_fbcopy;
extern IMGNODE bkgnd_fbdisplay;
IMGNODE userbkgnd_pic;
extern unsigned int display_tmp_addr;

#define FC 0xFF00
#define BC 0x0000

void PrintScreen(char * s)
{
	switch(G_DispMode)
	{
		case dmode_0C:
			PrintScreen_0(s);
			break;

		case dmode_90C:
			PrintScreen_90(s);
			break;

		case dmode_180C:
			PrintScreen_180(s);
			break;

		case dmode_270C:
			PrintScreen_270(s);
			break;

		default:
			PrintScreen_0(s);
			break;
		
	}
}

void PrintScreen_0(char * s)
{
	unsigned int i,j;
	unsigned int k;
	unsigned int dp_x,dp_y;
	unsigned short * ptr;
	
	if(Cursor_x > (GUI_LCM_XMAX - 8))
	{
		Cursor_x = 0;
		Cursor_y += 16;

		if(Cursor_y > (GUI_LCM_YMAX - 16))
		{
			Cursor_x = 0;
			Cursor_y = 0;

			for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
			{
				userbkgnd_bmp[2*i] = BC/256;
				userbkgnd_bmp[2*i+1] = BC%256;
			}
		}
	}		

	while(*s)
	{
		dp_x = Cursor_x;
		dp_y = Cursor_y;

		if((*s == '\r') || (*s == '\n'))
		{
			Cursor_y += 16;

			if(Cursor_y > (GUI_LCM_YMAX - 16))
			{
				Cursor_x = 0;
				Cursor_y = 0;

				for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
				{
					userbkgnd_bmp[2*i] = BC/256;
					userbkgnd_bmp[2*i+1] = BC%256;
				}
			}

			Cursor_x = 0;
			if((*(s+1)) == '\n')
			{
				s++;
			}
		}
		else
		{
			k=*s;
			if (k>32) k-=32; else k=0;
			if(k>0x5e) k = 0x5e;

			for(i=0;i<16;i++)
			{
				ptr = (unsigned short *)&userbkgnd_bmp[i*8*2];
				for(j=0;j<8;j++)
				{
					if(asc16_0[k*16+i]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
				}

			}

			userbkgnd_pic.img_base = display_tmp_addr;
			userbkgnd_pic.img_xlen = 8;
			userbkgnd_pic.img_ylen = 16;
			userbkgnd_pic.img_bpp = 16;
			v4a_fillvram(userbkgnd_pic.img_base,8 * 16/2,(int *)(userbkgnd_bmp));
			v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(dp_x,dp_y));

			Cursor_x += 8;

			if(Cursor_x > (GUI_LCM_XMAX - 8))
			{
				Cursor_x = 0;
				Cursor_y += 16;

				if(Cursor_y > (GUI_LCM_YMAX - 16))
				{
					Cursor_x = 0;
					Cursor_y = 0;

					for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
					{
						userbkgnd_bmp[2*i] = BC/256;
						userbkgnd_bmp[2*i+1] = BC%256;
					}
				}
			}
		}
		s++;
	}	
}

void PrintScreen_90(char * s)
{
	
	unsigned int i,j;
	unsigned int k;
	unsigned int dp_x,dp_y;
	unsigned short * ptr;
	
	if(Cursor_x > (GUI_LCM_YMAX - 8))
	{
		Cursor_x = 0;
		Cursor_y += 16;

		if(Cursor_y > (GUI_LCM_XMAX - 16))
		{
			Cursor_x = 0;
			Cursor_y = 0;

			for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
			{
				userbkgnd_bmp[2*i] = BC/256;
				userbkgnd_bmp[2*i+1] = BC%256;
			}
		}
	}		

	while(*s)
	{
		dp_x = GUI_LCM_XMAX - Cursor_y - 16;
		dp_y = Cursor_x;

		if((*s == '\r') || (*s == '\n'))
		{
			Cursor_y += 16;

			if(Cursor_y > (GUI_LCM_XMAX - 16))
			{
				Cursor_x = 0;
				Cursor_y = 0;

				for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
				{
					userbkgnd_bmp[2*i] = BC/256;
					userbkgnd_bmp[2*i+1] = BC%256;
				}
			}

			Cursor_x = 0;
			if((*(s+1)) == '\n')
			{
				s++;
			}
		}
		else
		{
			k=*s;
			if (k>32) k-=32; else k=0;
			if(k>0x5e) k = 0x5e;

			for(i=0;i<8;i++)
			{
				ptr = (unsigned short *)&userbkgnd_bmp[i*16*2];
				
				for(j=0;j<8;j++)
				{
					if(asc16_90[k*16+i*2+1]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
				}
				
				for(j=0;j<8;j++)
				{
					if(asc16_90[k*16+i*2]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
				}
			
			}

			userbkgnd_pic.img_base = display_tmp_addr;
			userbkgnd_pic.img_xlen = 16;
			userbkgnd_pic.img_ylen = 8;
			userbkgnd_pic.img_bpp = 16;
			v4a_fillvram(userbkgnd_pic.img_base,8 * 16/2,(int *)(userbkgnd_bmp));
			v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(dp_x,dp_y));

			Cursor_x += 8;

			if(Cursor_x > (GUI_LCM_YMAX - 8))
			{
				Cursor_x = 0;
				Cursor_y += 16;

				if(Cursor_y > (GUI_LCM_XMAX - 16))
				{
					Cursor_x = 0;
					Cursor_y = 0;

					for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
					{
						userbkgnd_bmp[2*i] = BC/256;
						userbkgnd_bmp[2*i+1] = BC%256;
					}
				}
			}
		}
		s++;
	}	
}

void PrintScreen_180(char * s)
{
	unsigned int i,j;
	unsigned int k;
	unsigned int dp_x,dp_y;
	unsigned short * ptr;
	IMGNODE userbkgnd_pic;
	
	if(Cursor_x > (GUI_LCM_XMAX - 8))
	{
		Cursor_x = 0;
		Cursor_y += 16;

		if(Cursor_y > (GUI_LCM_YMAX - 16))
		{
			Cursor_x = 0;
			Cursor_y = 0;

			for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
			{
				userbkgnd_bmp[2*i] = BC/256;
				userbkgnd_bmp[2*i+1] = BC%256;
			}
		}
	}		

	while(*s)
	{
		dp_x = GUI_LCM_XMAX - Cursor_x - 8;
		dp_y = GUI_LCM_YMAX - Cursor_y - 16;

		if((*s == '\r') || (*s == '\n'))
		{
			Cursor_y += 16;

			if(Cursor_y > (GUI_LCM_YMAX - 16))
			{
				Cursor_x = 0;
				Cursor_y = 0;

				for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
				{
					userbkgnd_bmp[2*i] = BC/256;
					userbkgnd_bmp[2*i+1] = BC%256;
				}
			}

			Cursor_x = 0;
			if((*(s+1)) == '\n')
			{
				s++;
			}
		}
		else
		{
			k=*s;
			if (k>32) k-=32; else k=0;
			if(k>0x5e) k = 0x5e;

			for(i=15;;i--)
			{
				ptr = (unsigned short *)&userbkgnd_bmp[(15-i)*8*2];
				for(j=7;;j--)
				{
					if(asc16_0[k*16+i]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
					if(j ==0 )
						break;
				}
				if(i ==0 )
						break;
			}

			userbkgnd_pic.img_base = display_tmp_addr;
			userbkgnd_pic.img_xlen = 8;
			userbkgnd_pic.img_ylen = 16;
			userbkgnd_pic.img_bpp = 16;
			v4a_fillvram(userbkgnd_pic.img_base,8 * 16/2,(int *)(userbkgnd_bmp));
			v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(dp_x,dp_y));
			
			Cursor_x += 8;

			if(Cursor_x > (GUI_LCM_XMAX - 8))
			{
				Cursor_x = 0;
				Cursor_y += 16;

				if(Cursor_y > (GUI_LCM_YMAX - 16))
				{
					Cursor_x = 0;
					Cursor_y = 0;

					for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
					{
						userbkgnd_bmp[2*i] = BC/256;
						userbkgnd_bmp[2*i+1] = BC%256;
					}
				}
			}
		}
		s++;
	}	
}



void PrintScreen_270(char * s)
{
	unsigned int i,j;
	unsigned int k;
	unsigned int dp_x,dp_y;
	unsigned short * ptr;
	
	if(Cursor_x > (GUI_LCM_YMAX - 8))
	{
		Cursor_x = 0;
		Cursor_y += 16;

		if(Cursor_y > (GUI_LCM_XMAX - 16))
		{
			Cursor_x = 0;
			Cursor_y = 0;

			for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
			{
				userbkgnd_bmp[2*i] = BC/256;
				userbkgnd_bmp[2*i+1] = BC%256;
			}
		}
	}		

	while(*s)
	{
		dp_x = Cursor_y;
		dp_y = GUI_LCM_YMAX - Cursor_x -8;

		if((*s == '\r') || (*s == '\n'))
		{
			Cursor_y += 16;

			if(Cursor_y > (GUI_LCM_XMAX - 16))
			{
				Cursor_x = 0;
				Cursor_y = 0;

				for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
				{
					userbkgnd_bmp[2*i] = BC/256;
					userbkgnd_bmp[2*i+1] = BC%256;
				}
			}

			Cursor_x = 0;
			if((*(s+1)) == '\n')
			{
				s++;
			}
		}
		else
		{
			k=*s;
			if (k>32) k-=32; else k=0;
			if(k>0x5e) k = 0x5e;

			for(i=7;;i--)
			{
				ptr = (unsigned short *)&userbkgnd_bmp[(7-i)*16*2];
				for(j=7;;j--)
				{
					if(asc16_90[k*16+i*2]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
					
					if(j ==0 )
						break;		
				}
				
				for(j=7;;j--)
				{
					if(asc16_90[k*16+i*2+1]&(0x80>>j))
					{
						*ptr = FC;
					}
					else
					{
						*ptr = BC;
					}
					ptr++;
					
					if(j==0 )
						break;		
				}
				
				
				if(i ==0 )
						break;			
			}

			userbkgnd_pic.img_base = display_tmp_addr;
			userbkgnd_pic.img_xlen = 16;
			userbkgnd_pic.img_ylen = 8;
			userbkgnd_pic.img_bpp = 16;
			v4a_fillvram(userbkgnd_pic.img_base,8 * 16/2,(int *)(userbkgnd_bmp));
			v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(dp_x,dp_y));
			
			Cursor_x += 8;

			if(Cursor_x > (GUI_LCM_YMAX - 8))
			{
				Cursor_x = 0;
				Cursor_y += 16;

				if(Cursor_y > (GUI_LCM_XMAX - 16))
				{
					Cursor_x = 0;
					Cursor_y = 0;

					for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX;i++)
					{
						userbkgnd_bmp[2*i] = BC/256;
						userbkgnd_bmp[2*i+1] = BC%256;
					}
				}
			}
		}
		s++;
	}	
}


void lcd_vline(unsigned short x, unsigned short y, unsigned short len, unsigned short fc)
{
	unsigned short * ptr;
	IMGNODE userbkgnd_pic;

	if(x >= GUI_LCM_XMAX)
	{
		x = 0;
	}
	if(y >= GUI_LCM_YMAX)
	{
		y = 0;
	}

	
	userbkgnd_pic.img_base = display_tmp_addr;
	userbkgnd_pic.img_xlen = 2;
	userbkgnd_pic.img_ylen = len;
	userbkgnd_pic.img_bpp = 16;
	memset(userbkgnd_bmp,0,1366*768*4);
	ptr = (unsigned short *)userbkgnd_bmp;

	//printf("v len = %d \r\n",len);

	len = 2*len+1;
	while(len)
	{
		len--;
		*ptr = fc;
		ptr++;
	}
	
	v4a_fillvram(userbkgnd_pic.img_base,userbkgnd_pic.img_ylen,(int *)(userbkgnd_bmp));
	v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(x,y));

}

void lcd_hline(unsigned short x, unsigned short y, unsigned short len, unsigned short fc)
{
	unsigned short * ptr;
	IMGNODE userbkgnd_pic;

	if(x >= GUI_LCM_XMAX)
	{
		x = 0;
	}
	if(y >= GUI_LCM_YMAX)
	{
		y = 0;
	}

	userbkgnd_pic.img_base = display_tmp_addr;
	userbkgnd_pic.img_xlen = len;
	userbkgnd_pic.img_ylen = 1;
	userbkgnd_pic.img_bpp = 16;
	memset(userbkgnd_bmp,0,1366*768*4);
	ptr = (unsigned short *)userbkgnd_bmp;
	len = len+1;
	while(len)
	{
		len--;
		*ptr = fc;
		ptr++;
	}
	v4a_fillvram(userbkgnd_pic.img_base,userbkgnd_pic.img_xlen/2,(int *)(userbkgnd_bmp));
	v4a_copyimage(&userbkgnd_pic,&bkgnd_fbdisplay,gui_point(x,y));
}
void DrawVLine(unsigned short x, unsigned short y,unsigned short len, unsigned short fc)
{
	;
}

